package com.wbt.bambootown.ui.fragment;

import android.support.v7.app.AlertDialog;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.gyf.barlibrary.ImmersionBar;
import com.wbt.bambootown.FrameApp;
import com.wbt.bambootown.R;
import com.wbt.bambootown.ui.activity.FarmIntroduceActivity;
import com.wbt.bambootown.ui.activity.LoginActivity;
import com.wbt.bambootown.ui.activity.ReplacePwdActivity;
import com.wbt.bambootown.ui.view.popwindow.CustomPopWindow;
import com.wbt.bambootown.utils.ActivityManagerUtil;
import com.wbt.bambootown.utils.DensityUtil;
import com.wbt.bambootown.utils.EventCenter;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by wufan on 2018/5/15.
 */

public class MineFragment extends BaseFragment {
    @BindView(R.id.tv_account)
    TextView tvAccount;
    @BindView(R.id.ll_layout)
    LinearLayout mLayout;

    @Override
    protected boolean userDefaultStatus() {
        return true;
    }

    @Override
    protected void initView() {
        tvAccount.setText(FrameApp.user.getUsername());
    }

    @Override
    protected void initData() {

    }

    @Override
    protected void onFirstUserVisible() {

    }

    @Override
    protected void onUserVisible() {

    }

    @Override
    protected void onUserInvisible() {

    }

    @Override
    public void initStatusBar(ImmersionBar immersionBar) {

    }

    @Override
    public int getContentView() {
        return R.layout.fragment_mine;
    }

    @Override
    protected boolean isApplyEventBus() {
        return false;
    }

    @Override
    protected boolean isApplyButterKnife() {
        return true;
    }

    @Override
    protected void onEventCome(EventCenter eventCenter) {

    }

    @OnClick({R.id.tv_replace_pwd, R.id.tv_farms_info,
            //R.id.tv_seeting,
            R.id.tv_exit_account})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_replace_pwd:
                startActivity(ReplacePwdActivity.class);
                break;
            case R.id.tv_farms_info:
                startActivity(FarmIntroduceActivity.class);
                break;
//            case R.id.tv_seeting:
//                break;
            case R.id.tv_exit_account:
                showPopListView();
                break;
        }
    }

    private void showPopListView() {
        //创建并显示popWindow
        View popView = LayoutInflater.from(getContext()).inflate(R.layout.layout_exit, null);

        final AlertDialog dialog = new AlertDialog.Builder(getContext())
                .setView(popView)
                .create();

        dialog.show();

        WindowManager.LayoutParams layoutParams = dialog.getWindow().getAttributes();
        layoutParams.width = (int) (DensityUtil.getScreenWidth(getContext()) * 0.7);
        layoutParams.height = (int) (DensityUtil.getScreenHeight(getContext()) * 0.2);
        dialog.getWindow().setAttributes(layoutParams);

        TextView mTvCancel = (TextView) popView.findViewById(R.id.tv_cancel);
        TextView mTvSure = (TextView) popView.findViewById(R.id.tv_sure);

        mTvCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.cancel();
            }
        });

        mTvSure.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                FrameApp.logout();
                startActivity(LoginActivity.class);
                ActivityManagerUtil.create().finishActivity();
            }
        });
    }
}
